<template>
  <div class="app-container">
    <el-row style="height: 800px">
      <el-col :span="5" class="app-container" style="border: 1px solid #DCDFE6;height: 100%;margin-right: 30px;border-radius: 25px">
        <el-button type="primary" style="margin-bottom: 20px;" @click="NewType = true">新增类别</el-button>
        <el-menu>
          <el-menu-item v-for="(item,index) in tableData" :key="index" @click="queryByIdTimetable(item)">
            <i class="el-icon-s-grid" />
            <span slot="title">{{ item.timetable_name }}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="18" style="border: 1px solid #DCDFE6;height:100%;border-radius: 25px" class="app-container">
        <el-col :span="24">
          <!-- <el-button plain type="success" class="el-icon-refresh" size="medium" @click="refreshData"></el-button> -->
          <el-button type="primary" style="margin-right: 10px;margin-bottom: 10px;margin-top: 10px" @click="dialog1 = true">新增</el-button>说明：【起始日期】必须是每周一，【结束日期】必须是每周日。
        </el-col>
        <el-dialog title="新增学期" :visible.sync="dialog1" width="300px">
          <el-form ref="addData" :model="addData" :rules="rules" label-width="80px">
            <el-form-item label="课程名称" prop="timetable_name">
              <el-input v-model="addData.timetable_name" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="起始时间" prop="begin_time">
              <el-time-picker v-model="addData.begin_time" placeholder="选择时间" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%;" />
            </el-form-item>
            <el-form-item label="结束时间" prop="end_time">
              <el-time-picker v-model="addData.end_time" placeholder="选择时间" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%;" />
            </el-form-item>
            <el-form-item label="作息时间" prop="timetable_id">
              <el-select v-model="addData.timetable_id" placeholder="请选择">
                <el-option v-for="(item,index) in tableData" :key="index" :label="item.timetable_name" :value="item.unid">{{ item.timetable_name }}</el-option>
              </el-select>
            </el-form-item>
            <div style="text-align: center">
              <el-button type="primary" @click="addTimetableMain('addData')">提交</el-button>
            </div>
          </el-form>
        </el-dialog>
        <el-dialog title="新增作息时间" :visible.sync="NewType" width="300px">
          <div style="text-align: center">
            <el-row>
              <el-col :span="24">
                <el-form ref="time" :model="time" :rules="rules" label-width="90px">
                  <el-form-item label="时间段名称" label-width="100px" prop="timetable_name2">
                    <el-input v-model="time.timetable_name2" />
                  </el-form-item>
                </el-form>
              </el-col>
              <el-button type="primary" @click="addTime('time')">提交</el-button>
            </el-row>
          </div>
        </el-dialog>
        <el-table :data="queryData" border style="width: 100%" height="592">
          <el-table-column type="index" label="序号" width="55" align="center" prop="index"></el-table-column>
          <el-table-column align="center" prop="timetable_name" label="课程名称" :show-overflow-tooltip="true" />
          <el-table-column align="center" prop="begin_time" label="开始时间" :show-overflow-tooltip="true" />
          <el-table-column align="center" prop="end_time" label="结束时间" :show-overflow-tooltip="true" />
          <el-table-column align="center" label="操作" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button size="small" type="success" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" @click="handleClick(scope.row.unid)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-dialog title="编辑" :visible.sync="editDig" width="300px" append-to-body>
      <el-form ref="editData" :model="editData" :rules="rules" label-width="80px">
        <el-form-item label="课程名称">
          <el-input v-model="editData.timetable_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="起始时间">
          <el-time-picker v-model="editData.begin_time" placeholder="选择时间" format="HH:mm:ss" value-format="HH:mm:ss"
            style="width: 100%;" />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-time-picker v-model="editData.end_time" placeholder="选择时间" format="HH:mm:ss" value-format="HH:mm:ss"
            style="width: 100%;" />
        </el-form-item>
        <el-form-item label="作息时间">
          <el-select v-model="editData.timetable_id" placeholder="请选择">
            <el-option v-for="(item,index) in tableData" :key="index" :label="item.timetable_name" :value="item.unid">{{ item.timetable_name }}</el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="editD">提交</el-button>
        </div>
      </el-form>
    </el-dialog>
    <el-dialog title="查看学期" :visible.sync="dialog3" width="300px" style="margin-top: 200px;">
      <el-form>
        <el-row>
          <el-col :span="24">
            <el-form-item label="课程名称" label-width="80px">
              <el-input v-model="querySem.timetable_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="起始日期" label-width="80px">
              <el-input v-model="querySem.begin_time" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="结束日期" label-width="80px">
              <el-input v-model="querySem.end_time" disabled />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="作息时间" label-width="80px">
              <el-input v-model="querySem.timetable_name2" disabled />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import {
    queryTimetable,
    addTimetable,
    addTimetableMain,
    queryByIdTimetable,
    deleTime
  } from '@/api/studentRecruit.js'
  export default {
    inject:['reload'],//页面刷新
    name: 'Index',
    data() {
      return {
        semesterRow: null,
        dialog3: false,
        querySem: {
          timetable_name: null,
          begin_time: null,
          end_time: null,
          timetable_name2: null
        },
        yan: {
          restTime: null,
          lessName: null,
          stratTime: null,
          endTime: null
        },
        editDig: false,
        queryName: null,
        queryData: [],
        editData: {
          unid: null,
          timetable_name: null,
          begin_time: null,
          end_time: null,
          timetable_id: null
        },
        addData: {
          timetable_name: null,
          begin_time: null,
          end_time: null,
          timetable_id: null
        },
        queryTableData: null,
        dialog1: false,
        time: {
          timetable_name2: null
        },
        tableData: [],
        NewType: false,
        checked: false,
        AddData: false,
        tableData2: [{
          id: 1,
          name: '哈哈',
          danwei: '前端'
        }],
        rules: {
          timetable_name: [{
            required: true,
            message: '请输入学期名称',
            trigger: 'blur,change'
          }],
          begin_time: [{
            required: true,
            message: '请选择起始时间',
            trigger: 'blur,change'
          }],
          end_time: [{
            required: true,
            message: '请选择结束事件',
            trigger: 'blur,change'
          }],
          timetable_id: [{
            required: true,
            message: '请选择作息事件',
            trigger: 'blur,change'
          }],
          timetable_name2: [{
            required: true,
            message: '请输入时间段名称',
            trigger: 'blur,change'
          }]
        }
      }
    },
    created() {
      this.queryTimetableList()
    },
    methods: {
      // 删除
      handleClick(unid) {
          this.$confirm('此操作将删除该课程，是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
              .then(async() => {
                  let query = {
                      unid: ''
                  }
                  query.unid = unid
                  deleTime(query).then(rs => {
                    if(rs.code != 200) return
                    else{
                      this.$message.success('删除学期成功')
                      this.queryTimetableList()
                      this.$forceUpdate()
                    }
                  })
              })
              .catch(() => {
                  this.$message({
                      type: 'info',
                      message: '已取消删除'
                  })
              })
      },
      // 编辑
      edit(row) {
        this.editDig = true
        console.log(row)
        this.editData = {
          unid: row.unid,
          timetable_name: row.timetable_name,
          begin_time: row.begin_time,
          end_time: row.end_time,
          timetable_id: row.timetable_id
        }
      },
      // 编辑数据
      editD() {
        this.editDig = false;
        console.log(this.editData)
        addTimetableMain(this.editData).then(rs => {
          if (rs.code == 200) {
            console.log(rs)
            // this.queryByIdTimetable()
            this.$message.success('编辑成功！')
            this.queryTimetableList()
            this.$forceUpdate()
          } else {
            this.$message.success('编辑失败！')
          }
        })
      },
      // 获取信息 - 时间表名字(Timetable)主键
      async queryByIdTimetable (item) {
        console.log(item)
        let queryData = {
          unid: item.unid
        }
        let resData = await queryByIdTimetable(queryData)
        if (resData.code == 200) {
          // this.$message.success('查询成功！')
          this.queryName = resData.data
          this.queryData = resData.data[0].main
          console.log(this.queryName)
          this.queryTimetableList()
          this.$forceUpdate()
        } else {
          this.$message.error('查询失败！')
        }
        console.log('resData', resData.data[0])
      },
      // 侧边栏所有数据 - 时间表名字(Timetable)列表
      async queryTimetableList () {
        let queryData = {}
        let resData = await queryTimetable(queryData)
        if (resData.code == 200) {
          this.tableData = resData.data
          // this.$message.success('查询成功！')
          console.log(resData.data)
          this.$forceUpdate()
        } else {
          this.$message.error('查询失败！')
        }
      },
      // 新增作息时间
      addTime(time) {
        this.$refs[time].validate((valid) => {
          if (valid) {
            this.NewType = false
            let addData = {
              timetable_name: this.time.timetable_name2
            }
            addTimetable(addData).then(resData => {
              if (resData.code == 200) {
                console.log(resData)
                this.$message.success('添加成功')
                this.queryTimetableList()
                this.$forceUpdate()
              } else {
                this.$message.error('添加失败')
              }
            })
          } else {
            console.log(valid)
            return false
          }
        })
      },
      // 新增学期
      addTimetableMain(addData) {
        this.$refs[addData].validate((valid) => {
          if (valid) {
            let beginTime = new Date(this.addData.begin_time)
            let endTime = new Date(this.addData.end_time)
            if (+endTime <= +beginTime) {
              this.$message.error('结束时间必须大于起始时间')
            } else if (+endTime > +beginTime) {
              addTimetableMain(this.addData).then(resData => {
                if (resData.code == 200) {
                  console.log('resData', resData)
                  this.$message.success('添加成功！')
                  this.dialog1 = false
                  this.$refs[addData].resetFields()
                  this.queryTimetableList()
                  this.$forceUpdate()
                } else {
                  this.$message.error('添加失败！')
                }
              })
            }
          } else {
            console.log(valid)
            return false
          }
        })
      },
      // 刷新
      // refreshData() {
      // 	// 重新请求数据
      //   this.queryTimetableList()
      //   this.$forceUpdate()
      // 	// this.$refs.tableRef.handleCurrentChange(1);
      // },
    }
  }
</script>
<style scoped>
</style>
